package com.atpy.reggie.controller;

import com.atpy.reggie.common.BaseContext;
import com.atpy.reggie.common.R;
import com.atpy.reggie.dto.OrdersDto;
import com.atpy.reggie.entity.OrderDetail;
import com.atpy.reggie.entity.Orders;
import com.atpy.reggie.entity.ShoppingCart;
import com.atpy.reggie.service.OrderDetailService;
import com.atpy.reggie.service.OrdersService;
import com.atpy.reggie.service.ShoppingCartService;
import com.atpy.reggie.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderDetailService orderDetailService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }
//localhost:8080/order/userPage?page=1&pageSize=5

    @GetMapping("/userPage")
    public R<Page> userpage(int page,int pageSize){
        final Page<Orders> ordersPage = new Page<>(page,pageSize);
        final Page<OrdersDto> ordersDtoPage = new Page<>();
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage,queryWrapper);
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        final List<Orders> ordersPageRecords = ordersPage.getRecords();
        List<OrdersDto> list2=ordersPageRecords.stream().map((item)->{
            final OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            final Long itemId = item.getId();
            LambdaQueryWrapper<OrderDetail> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId,itemId);
            final List<OrderDetail> list1 = orderDetailService.list(queryWrapper1);
            ordersDto.setOrderDetails(list1);
            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(list2);
        return R.success(ordersDtoPage);
    }

//    Request URL: http://localhost:8080/order/page?page=1&pageSize=10
//beginTime/endTime
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info(" page --> param: beginTime:{}, endTime:{}",beginTime,endTime);
        final Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(number!=null,Orders::getId,number);
//        SimpleFormatter simpleFormatter = new SimpleFormatter("yyyy-MM-dd HH:mm:ss");
//        simpleFormatter.format()
        if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                queryWrapper.ge(Orders::getOrderTime, simpleDateFormat.parse(beginTime));
                queryWrapper.le(Orders::getOrderTime, simpleDateFormat.parse(endTime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage,queryWrapper);
        return R.success(ordersPage);

    }
//    Request URL: http://localhost:8080/order

    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        log.info("{}",orders);
        ordersService.updateById(orders);

        return R.success("配送成果");
    }
//    Request URL: http://localhost:8080/order/again

    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        final Long ordersId = orders.getId();
        LambdaQueryWrapper<OrderDetail> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId,ordersId);
        final List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper);
        List<ShoppingCart> list=orderDetails.stream().map((item)->{
            final ShoppingCart cart = new ShoppingCart();
            cart.setName(item.getName());
            cart.setImage(item.getImage());
            cart.setUserId(BaseContext.getCurrentId());
            if (item.getDishId()!=null){
                cart.setDishId(item.getDishId());
            }else {
                cart.setSetmealId(item.getSetmealId());
            }
            cart.setDishFlavor(item.getDishFlavor());
            cart.setNumber(item.getNumber());
            cart.setAmount(item.getAmount());
            cart.setCreateTime(LocalDateTime.now());
            return cart;
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(list);
        return R.success("再来一单成功");
    }

}
